/*
 * Created on Jan 25, 2009
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.querie.beans;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author manish.mittal
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SchemaBrowser {

	String sTxtSchemaBrowserChoice = "";
	String sTxtComponentChoice = "";
	String sTxtActivity = "";


	public static void main(String args[]) {
		SchemaBrowser browser = new SchemaBrowser();
		try {
			browser.setSTxtActivity("");
	        browser.setSTxtComponentChoice("");
	        browser.setSTxtSchemaBrowserChoice("VViews");
			ArrayList<String> alString = browser.getComponentList();
			
			Iterator<String> it = alString.iterator();
	        while(it.hasNext())
	        {
	        	System.out.println(it.next());
	        }
	        System.out.println();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @return Returns the sTxtComponentChoice.
	 */
	public String getSTxtComponentChoice() {
		return sTxtComponentChoice;
	}
	/**
	 * @param txtComponentChoice The sTxtComponentChoice to set.
	 */
	public void setSTxtComponentChoice(String txtComponentChoice) {
		sTxtComponentChoice = txtComponentChoice;
	}
	/**
	 * @return Returns the sTxtActivity.
	 */
	public String getSTxtActivity() {
		return sTxtActivity;
	}
	/**
	 * @param txtActivity The sTxtActivity to set.
	 */
	public void setSTxtActivity(String txtActivity) {
		sTxtActivity = txtActivity;
	}
	/**
	 * @return Returns the sTxtSchemaBrowserChoice.
	 */
	public String getSTxtSchemaBrowserChoice() {
		return sTxtSchemaBrowserChoice;
	}
	/**
	 * @param txtSchemaBrowserChoice The sTxtSchemaBrowserChoice to set.
	 */
	public void setSTxtSchemaBrowserChoice(String txtSchemaBrowserChoice) {
//		System.out.println("123 " +txtSchemaBrowserChoice);
		sTxtSchemaBrowserChoice = txtSchemaBrowserChoice;
	}

	public ArrayList getComponentList() throws Exception
	{
		ArrayList aList = new ArrayList();
		int iIndex = 0;
		int jIndex = 0;


        String data = URLEncoder.encode("n", "UTF-8") + "=" + URLEncoder.encode(sTxtSchemaBrowserChoice.substring(1), "UTF-8");
        data += "&" + URLEncoder.encode("t", "UTF-8") + "=" + URLEncoder.encode(sTxtSchemaBrowserChoice.substring(0,1), "UTF-8");

        if(sTxtSchemaBrowserChoice.equals("IIndices")) {
        	data = URLEncoder.encode("n", "UTF-8") + "=" + URLEncoder.encode("Tables", "UTF-8");
            data += "&" + URLEncoder.encode("t", "UTF-8") + "=" + URLEncoder.encode("U", "UTF-8");
        }

        String sURL = "http://cas.sdss.org/dr7/en/help/browser/shortdescr.asp?"+data;
        System.out.println(sURL);
        // Send data
        URL url = new URL(sURL);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;

        while ((line = rd.readLine()) != null) {

//        	System.out.println("line "+line);
        	iIndex = line.indexOf("'DESCR'>");
        	jIndex = line.indexOf("</a>");



        	if(iIndex != -1 && jIndex != -1)
        		aList.add(line.substring(iIndex+8, jIndex));

        }
        wr.close();
        rd.close();

        return aList;
	}


	public String getComponentDetails() throws Exception
	{
		String sData = "";

        String data = URLEncoder.encode("n", "UTF-8") + "=" + URLEncoder.encode(sTxtComponentChoice, "UTF-8");
        data += "&" + URLEncoder.encode("t", "UTF-8") + "=" + URLEncoder.encode(sTxtSchemaBrowserChoice.substring(0,1), "UTF-8");



        String sURL = "http://cas.sdss.org/dr7/en/help/browser/description.asp?"+data;
//        System.out.println(sURL);
        // Send data
        URL url = new URL(sURL);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        // Get the response
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;

        while ((line = rd.readLine()) != null) {

//        	System.out.println(line);
        	sData = sData + line;

        }
        int iIndex = sData.indexOf("<h1>");
        int jIndex = sData.indexOf("</BODY>");
        sData = sData.substring(iIndex, jIndex);

        wr.close();
        rd.close();
        return sData;
	}


}
